package com.xdf.ecommerce.service.communication;

import feign.Logger;
import feign.Request;
import feign.Retryer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

import static java.util.concurrent.TimeUnit.SECONDS;

/**
 * @author chanchaw
 * @create 2025-10-29 13:50
 */
@Configuration
public class FeignConfig {
    private static final int CONNECT_TIMEOUT_MILLS = 5000;// 连接的超时时长
    private static final int READ_TIMEOUT_MILLS = 5000;// 读取数据的超时时长
    /**
     * 开启 openFeign 日志
     * 注意需要调整模块的日志级别为 debug，feign才会打印日志
     * @return
     */
    @Bean
    public Logger.Level feignLogger(){
        return Logger.Level.FULL;
    }

    /**
     * 开启重试功能，下面介绍 return 中方法的三个参数：
     * period = 100ms, 每隔 100 ms 发起一个请求
     * maxPeriod = 1000ms, 发起请求的最大时间间隔(SECONDS.toMillis(1) = 1000ms)
     * maxAttempts = 5,最大发起5次请求
     * @return
     */
    @Bean
    public Retryer feignRetryer(){
        return new Retryer.Default(100, SECONDS.toMillis(1),5);
    }

    /**
     * 设置请求连接超时时长，响应超时时长
     * @return
     */
    @Bean
    public Request.Options options(){
        return new Request.Options(CONNECT_TIMEOUT_MILLS, TimeUnit.MICROSECONDS,READ_TIMEOUT_MILLS,TimeUnit.MILLISECONDS,true);
    }
}
